#libraries

library(readxl)
library(openxlsx)   
library(tidyverse)
library(FactoMineR)
library(factoextra)
library(ggplot2)
library(ggpubr)

#file

SPÖ <- read_excel("/journal_articles_2021/Pathways_Parliament/submit_SPSR/revise_resubmit/data/SPÖ_MCA.xlsx")


                                  ###############
                                  ###SPÖ#########
                                  ###############

# turn into factors

SPÖ <- SPÖ %>% 
  mutate_if(is.character,as.factor)

# data summary
summary(SPÖ)[, 3:20]

for (i in 3:20) {
  plot(SPÖ[,i], main=colnames(SPÖ)[i],
       ylab = "Count", col="steelblue", las = 2)
}


# visualize active variables

res.mca<-MCA(SPÖ,quali.sup=1:5)
print(res.mca)
plot(res.mca)

#results

var <- get_mca_var(res.mca)
var

#eigenvalues and modified rate

eig.val <- get_eigenvalue(res.mca)
eig.val

# screeplot

fviz_screeplot(res.mca, addlabels = TRUE, ylim = c(0, 20))

#contributions

head(round(var$contrib,2),50)

# MCA plot

fac_1_2  <- fviz_mca_var(res.mca, 
                            axes=c(1,2),
                            col.var = "black",
                            #col.quali.sup = "dimgray",
                            invisible=c("quali.sup"),
                            repel=TRUE,
                            labelsize = 3.0,
                            select.var=list(contrib=20), font.family = "sans")+
  theme_minimal()+
  labs(title = "SPÖ: categories")

fac_1_2

#clustering

res.hcpc <- HCPC(res.mca,order=FALSE) # 4 clusters

#dendrogram

fviz_dend(res.hcpc, show_labels = FALSE,
          palette = "Spectral")

#description by variable categories/individuals

res.hcpc$desc.var$category
res.hcpc$desc.ind

#save clusters

SPÖ_cluster<- res.hcpc$data.clust
SPÖ_cluster <- SPÖ_cluster %>%
  select(clust, everything())

SPÖ_cluster$LastName <- str_remove_all(SPÖ_cluster$LastName, "LastName_")
SPÖ_cluster$FirstName <- str_remove_all(SPÖ_cluster$FirstName, "FirstName_")

levels(SPÖ_cluster$clust)[levels(SPÖ_cluster$clust)=="1"] <- "Majors"
levels(SPÖ_cluster$clust)[levels(SPÖ_cluster$clust)=="2"] <- "Party Locals"
levels(SPÖ_cluster$clust)[levels(SPÖ_cluster$clust)=="3"] <- "Organised Labour Repr."
levels(SPÖ_cluster$clust)[levels(SPÖ_cluster$clust)=="4"] <- "Party Animals"

#visualize cluster

res.mca<-MCA(SPÖ_cluster,quali.sup=1:6)
print(res.mca)
plot(res.mca)

Cluster1_4_SPÖ <- fviz_pca_ind(res.mca, axes=c(1,2),label="none", habillage = "clust",
                           addEllipses=TRUE, ellipse.level=0.80, palette = "Spectral",font.family = "sans",legend.title = "Types")+
  theme_minimal()+
  labs(title = "SPÖ: individuals")

Cluster1_4_SPÖ

#visualize passive variables 

SPÖ_cp <- fviz_pca_ind(res.mca, axes=c(1,2),label="none", habillage = "Career Politicians",
                       addEllipses=TRUE, ellipse.level=0.70, palette = c("#999999", "#56B4E9"),font.family = "sans",legend.title = "Career Politicans")+
  theme_minimal()+
  labs(title = "SPÖ")

SPÖ_cp

SPÖ_ps <- fviz_pca_ind(res.mca, axes=c(1,2),label="none", habillage = "Public Sector Jobs",
                       addEllipses=TRUE, ellipse.level=0.70, palette = c("#999999", "#56B4E9"),font.family = "sans",legend.title = "Public Sector Jobs")+
  theme_minimal()+
  labs(title = "")

SPÖ_ps

SPÖ_w <- fviz_pca_ind(res.mca, axes=c(1,2),label="none", habillage = "Workers",
                      addEllipses=TRUE, ellipse.level=0.70, palette = c("#999999", "#56B4E9"),font.family = "sans",legend.title = "Workers")+
  theme_minimal()+
  labs(title = "")

SPÖ_w

#export file with clusters

write.xlsx(SPÖ_cluster, file = "/journal_articles_2021/Pathways_Parliament/submit_SPSR/revise_resubmit/data/SPÖ_cluster_final.xlsx")


                                    ###############
                                    ###export######
                                    ###############

ggarrange(factor_1_2, Cluster1_4_ÖVP,
          #labels = c("", ""),
          ncol = 2, nrow = 1) %>%
  ggexport(filename = "/journal_articles_2021/Pathways_Parliament/submit_SPSR/revise_resubmit/figures/figure3.png", width = 1200,
           height = 600,
           pointsize = 30)

ggarrange(fac_1_2, Cluster1_4_SPÖ,
          #labels = c("", ""),
          ncol = 2, nrow = 1) %>%
  ggexport(filename = "/journal_articles_2021/Pathways_Parliament/submit_SPSR/revise_resubmit/figures/figure4.png", width = 1200,
           height = 600,
           pointsize = 30)



ggarrange(ÖVP_cp,ÖVP_ps,ÖVP_f,SPÖ_cp,SPÖ_ps,SPÖ_w,
           ncol = 3, nrow = 2) %>%
  ggexport(filename = "/journal_articles_2021/Pathways_Parliament/submit_SPSR/revise_resubmit/figures/figure6.png", width = 1200,
           height = 600)

theme_set(theme_pubr())



